/*
 * @Author: stark
 * @Date: 2022-06-20 18:57:57
 */
$(function () {
    $.cookie.json = true;
    let back = $.getUrlParam('back');
    $("#send").on('click', () => {
        let mobile = $(".mobile").val();
        let password = $(".password").val();

        let data = {
            mobile,
            password
        };

        $.post(baseUrl + '/user/login', data, response => {
            if (response.code != 0) {
                layer.msg('手机号码/密码不正确，请从新输入');
                return false;
            }
            $.cookie('userInfo', response.data, { expires: 1 })

            if (localStorage.getItem('userCarts')) {
                let userInfo = $.cookie('userInfo') ? $.cookie('userInfo') : null;
                let userCarts = JSON.parse(localStorage.getItem('userCarts'))

                userId = userInfo.id;

                for (let k in userCarts) {
                    carts = [
                        { "goodsId": k, "goodsCount": userCarts[k] },
                    ]
                    // console.log(k, userCarts[k]);
                }
                // console.log(userId, carts);

                $.post(baseUrl + '/cart/addAll', { userId, carts }, response => {
                    localStorage.removeItem("userCarts");
                    location.href = 'car.html'
                })
            } else {
                if (back) {
                    location.href = decodeURIComponent(back);
                } else {
                    location.href = 'index.html'
                }
            }
        })
    })
})